package com.cxd.ancun.service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ancun.aosp.client.AospClient;
import com.ancun.aosp.dto.AospRequest;
import com.ancun.aosp.dto.AospResponse;
import com.ancun.aosp.dto.ClientUser;
import com.cxd.ancun.constants.AncunConstants;
import com.cxd.ancun.constants.InvestConstants.InvestStatus;
import com.cxd.ancun.entity.base.AncunOperation;
import com.cxd.ancun.entity.base.BankCard;
import com.cxd.ancun.entity.base.ContractBean;
import com.cxd.ancun.entity.base.CxdCode;
import com.cxd.ancun.entity.base.CxdMessage;
import com.cxd.ancun.entity.base.EnterpriseInfo;
import com.cxd.ancun.entity.base.Invest;
import com.cxd.ancun.entity.base.InvestRepay;
import com.cxd.ancun.entity.base.Loan;
import com.cxd.ancun.entity.base.LoanType;
import com.cxd.ancun.entity.base.Node;
import com.cxd.ancun.entity.base.Recharge;
import com.cxd.ancun.entity.base.User;
import com.cxd.ancun.entity.base.WithdrawCash;
import com.cxd.base.common.exception.CxdException;
import com.cxd.base.mapper.write.WManageMapper;
import com.cxd.base.util.ArithUtil;
import com.cxd.base.util.BeanTools;
import com.cxd.base.util.CxdStringUtil;
import com.cxd.base.util.date.DateStyle;
import com.cxd.base.util.date.DateUtil;
import com.cxd.base.util.pdf.PDFUtils;
import com.lowagie.text.DocumentException;

import freemarker.template.TemplateException;

/**
 * <一句话功能简述> <功能详细描述>
 * @author wtj
 * @date 2016年4月6日 下午2:47:01
 * @company CXD
 * @version [Copyright (c) 2016 V001]
 */
@Service("ancunService")
public class AncunService {
	
	private static final Logger logger = Logger.getLogger(AncunService.class);
	@Resource
	public BeanTools db;
	@Autowired
	private AospClient aospClient;
	@Autowired
	private DictService dictService;
	@Resource
	private LoanService loanService;
	@Resource
	private InvestService investService;
	@Resource
	private WManageMapper wmanageMapper ;
	
	
	public void saveAncunSend(String ancun_operation_id){
		AncunOperation ao = db.queryBean("ancun_operation", "id", ancun_operation_id, AncunOperation.class);
		if(ao.getRecordNo()!=null && !ao.getRecordNo().trim().equals("")){
			throw new CxdException("保全号不为空,已经完成保全");
		}
		AospRequest aospRequest = new AospRequest();
		CxdMessage cm = this.getAosRequest(aospRequest, ao);
		if(cm.getCode() != null && cm.getCode().equals(CxdCode.CODE_500)){
			ao.setResponseData(cm.getMsg());
			ao.setStatus(Integer.parseInt(CxdCode.CODE_500.getCode()));
		}else{
			AospResponse aospResponse = aospClient.save(aospRequest);
			// 操作成功
			if(aospResponse.getCode() == AncunConstants.ancunStatusCode.C100000){
				Map<String, String> resultMap = aospResponse.getData();
				String recordNo = resultMap.get("recordNo");
				ao.setStatus(AncunConstants.ancunSendStatus.SUCCESS);
				ao.setRecordNo(recordNo);
			}else{
				ao.setStatus(AncunConstants.ancunSendStatus.FAIL);
			}
			ao.setResponseData(JSON.toJSONString(aospResponse));
		}
		ao.setResponseTime(DateUtil.dateToString(new Date(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
		wmanageMapper.updateAncunSend(ao);
	}
	
	public CxdMessage getAosRequest(AospRequest aospRequest,AncunOperation aob){
		CxdMessage cm = new CxdMessage();
		aospRequest.setItemKey(aob.getItemKey());
		aospRequest.setFlowNo(aob.getFlowNo());
		// 投资
		if(aospRequest.getItemKey().equals(AncunConstants.itemKey.INVEST)){
			//   投资交易信息保全
			if(AncunConstants.investFlowNo.INVEST.equals(aob.getFlowNo())){
				this.setInvestInfo(aospRequest,aob);
			}else{
				Invest inv = this.getInvestBaseInfo(aob.getCxdId());
				// 投资人用户ID
				String investUserId = inv.getUserId();
				User ub = this.getUserById(investUserId);
				if(StringUtils.isBlank(ub.getRealname()) || StringUtils.isBlank(ub.getIdCard()) || StringUtils.isBlank(ub.getMobileNumber())){
					cm.setCode(CxdCode.CODE_500);
					cm.setMsg("用户信息不完整");
					return cm;
				}
				this.investAncunFlow(aospRequest, aob, investUserId, ub);
			}
		}
		// 充值
		else if(aospRequest.getItemKey().equals(AncunConstants.itemKey.RECHARGE)){
			this.setAncunRechargeInfo(aospRequest,aob.getCxdId());
		}
		// 提现
		else if(aospRequest.getItemKey().equals(AncunConstants.itemKey.WITHDRAW)){
			this.setAncunWithdrawInfo(aospRequest,aob.getCxdId());
		}
		// 借款保存
		else if(aospRequest.getItemKey().equals(AncunConstants.itemKey.LOAN)){
			this.loanAncunFlow(aospRequest, aob);
		}
		
		ClientUser cu = aospRequest.getClientUser();
			aob.setItemKey(aospRequest.getItemKey());
			aob.setFlowNo(aob.getFlowNo());
			aob.setRequestTime(DateUtil.dateToString(new Date(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
			aob.setRequestData(JSON.toJSONString(aospRequest));
			aob.setIdCard(cu.getIdcard());
			aob.setMobile(cu.getMobile());
			aob.setStatus(AncunConstants.ancunSendStatus.INIT);
		wmanageMapper.updateAncunSend(aob);
		return cm;
	}
	
	private void setClientUser(AospRequest aospRequest,User ub){
		// 以下是业务数据的用户信息
		ClientUser clientUser = new ClientUser();
		clientUser.setMobile(ub.getMobileNumber());
		clientUser.setIdcard(ub.getIdCard());
		clientUser.setTruename(ub.getRealname());
		aospRequest.setClientUser(clientUser);
	}
	
	/**
	 * 借款安存保存
	 * <一句话功能简述> <功能详细描述>
	 * @author wtj
	 * @date 2016年5月12日上午10:16:17
	 * @param aospRequest
	 * @param aob
	 */
	private void loanAncunFlow(AospRequest aospRequest,AncunOperation aob){
		String loanId = aob.getCxdId();
		Loan lo = this.getLoanInfo(loanId);
		User ub = this.getUserById(lo.getUserId());
		// 以下是业务数据的用户信息
		this.setClientUser(aospRequest,ub);
		// 募集结束日期追加
		if(AncunConstants.loanFlowNo.LOAN.equals(aob.getFlowNo())){
			this.setLoanPersonInfo(aospRequest, lo);
			this.setLoanInfo(aospRequest, lo,lo.getInterestBeginTime());
		}else if(AncunConstants.loanFlowNo.ADD_LOAN_END_TIME.equals(aob.getFlowNo())){
			List<Map<String,Object>> acList = db.queryListMap(String.format("select record_no from ancun_operation where cxd_id ='%s' and flow_no='%s'",aob.getCxdId(),AncunConstants.loanFlowNo.LOAN));
			if(acList!=null && acList.size()>0){
				Object rn = acList.get(0).get("record_no");
				aospRequest.setRecordNo(rn!=null?rn.toString():"");
			}
			int investCoutn = db.queryForInt(String.format("select count(0) from invest where loan_id = '%s' and status not in ('cancel')",loanId));
			// 投资人数
			aospRequest.addField("investCount",investCoutn);
			// 实际融资额
			aospRequest.addField("loanInvestMoney",lo.getMoney());
			// 募集结束日期
			aospRequest.addField("raiseEndTime", DateUtil.dateToString(aob.getCtime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
		}
	}
	/**
	 * 投资安存保存
	 * <一句话功能简述> <功能详细描述>
	 * @author wtj
	 * @date 2016年5月12日上午10:16:17
	 * @param aospRequest
	 * @param aob
	 */
	private void investAncunFlow(AospRequest aospRequest,AncunOperation aob,String investUserId,User ub){
		// 以下是业务数据的用户信息
		this.setClientUser(aospRequest,ub);
		// 获取投资时产生的保全号
		AncunOperation investRAP = db.queryBean("ancun_operation", new String[]{"cxd_id","flow_no"},new String[]{aob.getCxdId(),AncunConstants.investFlowNo.INVEST}, AncunOperation.class);
		if(investRAP==null || StringUtils.isBlank(investRAP.getRecordNo())){
			logger.error(String.format("提交保全信息异常:保全信息%S",JSON.toJSON(aob)));
			investRAP = new AncunOperation();
			// throw new CxdException("提交保全信息异常");
		}
		aospRequest.setRecordNo(investRAP.getRecordNo());
		// 募集结束日期追加
		if(AncunConstants.investFlowNo.ADD_LOAN_END_TIME.equals(aob.getFlowNo())){
			aospRequest.addField("raiseEndTime", DateUtil.dateToString(aob.getCtime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
		}
		//   投资协议追加
		else if(AncunConstants.investFlowNo.ADD_CONTRACT.equals(aob.getFlowNo())){
			String investId = aob.getCxdId();
			Map<String,Object> loan = db.queryMap(String.format("select loan.id,loan.money,loan.deadline,loan.INTEREST_BEGIN_TIME interestBeginTime,loan.USER_ID userId,loan.TYPE type,loan.rate,"
														+ " loan.CONTRACT_TYPE contractType,loan.GIVE_MONEY_TIME giveMoneyTime,loan.INTEREST_BEGIN_TIME interestBeginTime"
														+ " from loan inner join invest on loan.id = invest.loan_id where invest.id = '%s'",investId));
			// 借款用户ID
			String loanUserId = loan.get("userId").toString();
			String loanId = loan.get("id").toString();
			
			LoanType loanType = this.getLoanTypeByLoanId(loan.get("type").toString());
			
			loan.put("deadlinePeriod",Integer.parseInt(loan.get("deadline").toString())*loanType.getRepayTimePeriod());
			String unit = dictService.getValue("repay_unit",loanType.getRepayTimeUnit());
			if(StringUtils.isNotEmpty(unit)){
				loan.put("deadlineUnit",unit);
			}
			
			Date interestEndTime = null;
			if("day".equals(loanType.getRepayTimeUnit())){
				interestEndTime = DateUtil.addDay(DateUtil.toDate(loan.get("interestBeginTime").toString()),Integer.parseInt(loan.get("deadline").toString()));
			}else{
				interestEndTime = DateUtil.addMonth(DateUtil.toDate(loan.get("interestBeginTime").toString()),Integer.parseInt(loan.get("deadline").toString()));
			}
			
			loan.put("interestEndTime",interestEndTime);//DateUtil.dateToString(interestEndTime,DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
			loan.put("ratePercent",Double.parseDouble(loan.get("rate").toString())*100);
			loan.put("dbTime", new Date());
			User loanUser = this.getUserById(loanUserId);
			
			if(StringUtils.isNotEmpty(loanUser.getIdCard())&&loanUser.getIdCard().length()>10){
				loanUser.setIdCard(loanUser.getIdCard().substring(0,8)+"********"+loanUser.getIdCard().substring(loanUser.getIdCard().length()-2));
			}
			
			String sql = "select t.IS_AUTO_INVEST as isAutoInvest,t.id from invest t where t.loan_id = '%s' and t.status != '%s' and t.user_id = '%s'";
			List<Map<String,Object>> investLists = db.queryListMap(String.format(sql,loanId, InvestStatus.CANCEL,investUserId));
			for(Map<String,Object> invest:investLists){
				invest.put("loan", loan);
				invest.put("investRepayRoadmap", investService.getRepayRoadmap(invest.get("id").toString()));
			}
			Map<String, Object> root = new HashMap<String,Object>();
			root.put("investLists", investLists);
			root.put("loan", loan);
			root.put("loanUser", loanUser);
			root.put("loanRepayRoadmap", loanService.getRepayRoadmap(loanId));
			List<Map<String,Object>> loanRepays = db.queryListMap(String.format("select * from loan_repay  where loan_id = '%s'",loanId) );
			root.put("loanRepays", loanRepays);
			root.put("curUser", ub);

			ContractBean cb = new ContractBean();
			try {
				cb.setInvestId(investId);
				cb.setContractType(loan.get("contractType").toString());
				PDFUtils.createContractHtml(root,cb);
				logger.error("pdf访问地址:"+cb.getPdfUrl());
			} catch (IOException e) {
				logger.error("类AncunService.getAosRequest", e);
				throw new CxdException("IOException 异常",e);
			} catch (TemplateException e) {
				logger.error("类AncunService.getAosRequest", e);
				throw new CxdException("TemplateException 异常",e);
			} catch (DocumentException e) {
				logger.error("类AncunService.getAosRequest", e);
				throw new CxdException("DocumentException 异常",e);
			}
			aospRequest.addFileByUrl(cb.getPdfUrl());
		}
		//   实际回款清单追加
		else if(AncunConstants.investFlowNo.ADD_REPAY.equals(aob.getFlowNo())){
			InvestRepay irb = db.queryBean("invest_repay", new String[]{"invest_id","period"}, new String[]{aob.getCxdId(),aob.getPeriod()}, InvestRepay.class);
			// 以下是业务数据的用户信息
			aospRequest.addField("repayId", irb.getId());
			aospRequest.addField("repayTime", DateUtil.dateToString(irb.getTime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()) );
			aospRequest.addField("repayMoney", ArithUtil.add(Double.parseDouble(irb.getCorpus()),Double.parseDouble(irb.getInterest())));
		}
	}
	/**
	 * 保全投资 第一步
	 * @author wtj
	 * @date 2016年4月7日上午11:30:11
	 * @param aospRequest
	 * @param investId
	 */
	public void setInvestInfo(AospRequest aospRequest,AncunOperation aob){
		Invest inv = this.getInvestBaseInfo(aob.getCxdId());
		// 用户ID
		String userId = inv.getUserId();
		// 借款ID
		String loanId = inv.getLoanId();
		User ub = this.getUserById(userId);
		Loan lo = this.getLoanInfo(loanId);

		// 以下是业务数据的用户信息
		this.setClientUser(aospRequest,ub);
		this.setLoanPersonInfo(aospRequest, lo);
		this.setLoanInfo(aospRequest, lo,lo.getInterestBeginTime());
		// 用户真实姓名
		aospRequest.addField("investUserName",ub.getRealname());
		// 投资人手机号
		aospRequest.addField("investPhoneId", ub.getMobileNumber());
		// 投资人身份证号
		aospRequest.addField("investIdCard", ub.getIdCard());
		// 注册时间
		aospRequest.addField("investRegTime", DateUtil.dateToString(ub.getRegisterTime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()) );
		// 认证成功时间
		aospRequest.addField("investAuthTime", DateUtil.dateToString(new Date(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()) );

		
		// 投资账户id
		aospRequest.addField("investUserId", inv.getUserId());
		// 投资金额
		aospRequest.addField("investMoeny", inv.getMoney());
		// 红包抵扣金额
		aospRequest.addField("redDeduction",AncunConstants.RED_DEDUCTION);
		// 购买时间
		aospRequest.addField("investTime", DateUtil.dateToString(inv.getTime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()) );
		// 支付成功时间
		aospRequest.addField("investPaymentTime",DateUtil.dateToString(aob.getCtime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
	}
	
	/**
	 * 设置借款人信息
	 * @author wtj
	 * @date 2016年5月12日上午10:22:26
	 * @param aospRequest
	 * @param lo
	 */
	private void setLoanPersonInfo(AospRequest aospRequest,Loan lo){
		User loanUser = this.getUserById(lo.getUserId());
		// 借款人信息
		aospRequest.addField("loanUserName", CxdStringUtil.nameAddStar(loanUser.getRealname()));
		// 身份证
		aospRequest.addField("loanIdCard", CxdStringUtil.strAddStar(loanUser.getIdCard(), 2, 3, 5) );
		// 联系电话
		aospRequest.addField("loanPhoneId", CxdStringUtil.strAddStar(loanUser.getMobileNumber(), 2, 3, 5));
		// 联系地址
		aospRequest.addField("loanUserAddress", "");
		// 抵押信息
		aospRequest.addField("loanMortgage", this.getLoanMortgage(lo.getDescription()));
	}
	
	private void setLoanInfo(AospRequest aospRequest,Loan lo,Date interestTime){
		LoanType loanType =  this.getLoanType(lo.getType());
		// 项目编号
		aospRequest.addField("loanNo", lo.getId());
		// 年化收益率
		aospRequest.addField("loanRate", (lo.getRate()*100+"%"));
		// 项目总额
		aospRequest.addField("loanMoney", lo.getLoanMoney());
		// 项目期限
		aospRequest.addField("loanDay", getLoanDay(lo.getDeadline(),loanType.getRepayTimeUnit())+"天");
		// 还款方式
		aospRequest.addField("loanRepayType", this.getLoanType(lo.getType()).getDescription());
		// 发布时间
		aospRequest.addField("releaseTime", DateUtil.dateToString(lo.getVerifyTime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()) );
		
		if(lo.getType().equals("loan_type_4")||lo.getType().equals("loan_type_5")){
			aospRequest.addField("interestTime", DateUtil.dateToString(interestTime,DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()) );
		}else{
			aospRequest.addField("interestTime", DateUtil.dateToString(lo.getVerifyTime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()) );
		}
		// 起息日期
//		Date interestTime;
//		// 即为还款日。如不填写，则默认放款当天；如果在放款之前计算预计利息，则该值取预计执行时间。
//		if(lo.getInterestBeginTime()==null || new Date().getTime()>lo.getInterestBeginTime().getTime()){
//			interestTime = lo.getInterestBeginTime();//new Date();
//		}else{
//			interestTime = lo.getExpectTime();
//		}
		
//		if(lo.getType().equals("loan_type_5") || lo.getType().equals("loan_type_1")){
//			// 投资时间
//		}else{
//			
//		}
		
		
		// 起投金额
		aospRequest.addField("smallBeginMoney", lo.getMinInvestMoney());
		// 担保公司
		aospRequest.addField("ensureCompany", this.getEnsureCompany(lo.getGuaranteeCompanyName()).getTitle());

	}
	
	// 获取借款抵押信息
	public String getLoanMortgage(String loanDescription){
		String dywStr = "";
		String descStr = loanDescription.replaceAll("<[^>]+>|&nbsp;", "");
		String[] strArr = descStr.split("【");
		for (String str : strArr) {
			String[] thArr = str.split("】");
			if (thArr.length == 2) {
				if(thArr[0].toString().indexOf("抵押物") >= 0){
					dywStr = thArr[1];
					break;
				}
			}
		}
		return dywStr;
	}
	
	/**
	 * 获取投资天数
	 * @author wtj
	 * @date 2016年4月7日下午1:20:39
	 * @param deadline
	 * @param dayType
	 * @return
	 */
	public String getLoanDay(String deadline,String dayType){
		if(dayType.equals("month")){
			deadline = (Integer.parseInt(deadline)*30)+"";
		}
		return deadline;
	}
	/**
	 * 获取担保公司
	 * @author wtj
	 * @date 2016年4月7日下午1:18:48
	 * @param nodeId
	 * @return
	 */
	public Node getEnsureCompany(String nodeId){
		if(StringUtils.isBlank(nodeId)){
			return new Node();
		}
		return db.queryBean("node","id",nodeId, Node.class);
	}
	
	/**
	 * 获取借款类型
	 * @author wtj
	 * @date 2016年4月7日下午1:16:19
	 * @param loanType
	 * @return
	 */
	public LoanType getLoanType(String loanType){
		return db.queryBean("LOAN_TYPE","id",loanType, LoanType.class);
	}
	
	/**
	 * 获取用户信息
	 * @author wtj
	 * @date 2016年4月6日下午5:01:12
	 * @param userId
	 * @return
	 */
	public User getUserById(String userId){
		User user = db.queryBean("USER", "id", userId, User.class);
		// 企业用户
		if(user.getIdentityType().equals("1")){
			EnterpriseInfo ei = db.queryBean("ENTERPRISE_INFO","ENTERPRISEID",userId, EnterpriseInfo.class);
			user.setRealname(ei.getName());
			user.setIdCard(ei.getIdCard());
			user.setMobileNumber(ei.getEmergencyPhone());
		}
		return user;
	}
	/**
	 * 借款信息
	 * @author wtj
	 * @date 2016年4月7日上午11:35:29
	 * @param longId
	 * @return
	 */
	public Loan getLoanInfo(String longId){
		return db.queryBean("LOAN", "id", longId, Loan.class);
	}
	/**
	 * 获取用户绑卡信息
	 * @author wtj
	 * @date 2016年4月6日下午5:06:40
	 * @param userId
	 * @return
	 */
	public BankCard getBankCardBaseInfo(String userId){
		List<BankCard> bcList = db.queryListBean(String.format("select * from BANK_CARD where user_id = '%s' and (status = 'VERIFIED' or status = '解绑审核中') ORDER BY time desc ",userId),BankCard.class);
		if(bcList!=null && bcList.size()>0){
			return bcList.get(0);
		}else{
			return new BankCard();
		}
	}
	/**
	 * 获取充值记录
	 * @author wtj
	 * @date 2016年4月6日下午5:30:29
	 * @param id
	 * @return
	 */
	public Recharge getRechargeBaseInfo(String id){
		return db.queryBean("RECHARGE", "id",id, Recharge.class);
	}
	
	/**
	 * 获取投资信息
	 * @author wtj
	 * @date 2016年4月7日上午11:29:35
	 * @param id
	 * @return
	 */
	public Invest getInvestBaseInfo(String id){
		return db.queryBean("INVEST", "id",id, Invest.class);
	}
	
	/**
	 * 设置安存充值信息
	 * @author wtj
	 * @date 2016年4月6日下午4:44:43
	 * @param aospRequest
	 * @param rechargeId
	 */
	public void setAncunRechargeInfo(AospRequest aospRequest,String rechargeId){
		Recharge rb = this.getRechargeBaseInfo(rechargeId);
		String userId = rb.getUserId();
		User ub = this.getUserById(userId);
		
		// 以下是业务数据的用户信息
		this.setClientUser(aospRequest,ub);
		
		// 充值账户ID
		aospRequest.addField("rechargeAccount", rb.getUserId());
		// 姓名
		aospRequest.addField("rechargeUserName", ub.getRealname());
		// 身份证号
		aospRequest.addField("rechargeIdCard", ub.getIdCard());
		// 充值渠道
		aospRequest.addField("rechargeChannel", AncunConstants.RECHARGE_CHANNEL);
		// 充值金额
		aospRequest.addField("rechargeAmount", rb.getActualMoney());
		// 充值时间
		aospRequest.addField("rechargeOperateTime",  DateUtil.dateToString(rb.getTime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()));
		// 充值成功时间 
		aospRequest.addField("rechargeSuccessTime",  DateUtil.dateToString(rb.getSuccessTime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()) );
		// 支付流水号 
		aospRequest.addField("rechargeSerialNo", rechargeId);
	}
	/**
	 * 设置安存提现信息
	 * @author wtj
	 * @date 2016年4月6日下午4:44:43
	 * @param aospRequest
	 * @param rechargeId
	 */
	public void setAncunWithdrawInfo(AospRequest aospRequest,String withdrawId){
		WithdrawCash wcb = db.queryBean("withdraw_cash", "id", withdrawId, WithdrawCash.class);
		String userId = wcb.getUserId();
		User ub = this.getUserById(userId);
		
		// 以下是业务数据的用户信息
		this.setClientUser(aospRequest,ub);
		
		BankCard bcb = getBankCardBaseInfo(userId);
		String withdrawBankName="";
		String withdrawBankAccount="";
		if(bcb!=null&&bcb.getBankNo()!=null&&bcb.getCardNo()!=null){
			withdrawBankName = AncunConstants.bankCodeNameMap.get(bcb.getBankNo().toUpperCase().trim());
			withdrawBankAccount = bcb.getCardNo();
			
		}
		// 投资账户ID
		aospRequest.addField("withdrawAccount", userId);
		// 用户真实姓名
		aospRequest.addField("withdrawUserName", ub.getRealname());
		// 身份证号
		aospRequest.addField("withdrawIdCard", ub.getIdCard());
		// 银行名称
		aospRequest.addField("withdrawBankName", withdrawBankName);
		// 银行账号
		aospRequest.addField("withdrawBankAccount", withdrawBankAccount);
		// 提现金额
		aospRequest.addField("withdrawAmount", wcb.getMoney());
		// 操作时间
		aospRequest.addField("withdrawOperateTime", DateUtil.dateToString(wcb.getTime(),DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()) );
		// 提现成功时间
		Date recheckTime = wcb.getRecheckTime();
		if(recheckTime == null){
			recheckTime = new Date();
		}
		aospRequest.addField("withdrawSuccessTime", DateUtil.dateToString(recheckTime,DateStyle.YYYY_MM_DD_HH_MM_SS.getValue()) );
	}
	/**
	 * 根据借款ID 获取借款信息
	 * @author wtj
	 * @date 2016年4月11日下午1:54:23
	 * @param investId
	 * @return
	 */
//	public Loan getLoanByInvestId(String investId){
//		Invest invest = db.queryBean("invest", "ID", investId, Invest.class);
//		return db.queryBean("loan", "ID", invest.getLoanId(), Loan.class);
//	}
	public LoanType getLoanTypeByLoanId(String type){
		return db.queryBean("LOAN_TYPE", "ID", type, LoanType.class);
	}
}
